package com.scoy.honeymei.fragment.mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.GoodsDetailActivity;
import com.scoy.honeymei.adapter.MallAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.base.FragmentLazy;
import com.scoy.honeymei.base.MeLazyFragment;
import com.scoy.honeymei.bean.CateBean;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.custem.CartEvent;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: GoodsListFragment$
 * @Description: 商城 列表
 * @Author: GaoZhan
 * @CreateDate: 2019/9/3/0003$ 上午 9:44$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/3/0003$ 上午 9:44$
 * @UpdateRemark: 更新内容
 */
public class GoodsListFragment extends /*BaseFragment*/ FragmentLazy {

    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private int type;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<GoodsBean> datalist;
    private MallAdapter mallAdapter;
    private int cateId;
    private int pageInt=0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            cateId = getArguments().getInt("cateId");
        }
    }

    public static GoodsListFragment newInstance(int type, int cateId) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putInt("cateId", cateId);
        GoodsListFragment fragment = new GoodsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /*  @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
          unbinder = ButterKnife.bind(this, view);
          mContext = getContext();

          initNormal();

          httpGoodsList();

          return view;
      }*/
    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();

        return view;
    }

    @Override
    protected void initData() {
        httpGoodsList(0);
    }

    public void initNormal() {
        datalist = new ArrayList<>();

        initRV();

        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                datalist.clear();
                httpGoodsList(0);

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);
                httpGoodsList(pageInt);
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void initRV() {
        mallAdapter = new MallAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(mallAdapter);
        mallAdapter.setOnOneClick(new MallAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                GoodsBean goodsBean = datalist.get(pos);
                int id = goodsBean.getId();

                Intent intentde = new Intent(mContext, GoodsDetailActivity.class);
                intentde.putExtra("goodsId", id + "");
                startActivityForResult(intentde, 0x871);
            }
        });
        mallAdapter.setOnTwoClick(new MallAdapter.OnTwoClick() {//加入购物车
            @Override
            public void twoClick(int pos) {
                GoodsBean goodsBean = datalist.get(pos);
                httpAddCart(goodsBean.getId());
            }
        });
    }


    private void httpGoodsList(int myPage) {
        HttpParams params = new HttpParams();
        params.put("id", cateId);
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.GOODS_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<GoodsBean> data = new Gson().fromJson(o, new TypeToken<List<GoodsBean>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(data);
                mallAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    //加入购物车http
    private void httpAddCart(int goodsId) {
        HttpParams params = new HttpParams();
        params.put("id", goodsId);
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("number", 1);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.ADD_TOCART, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                EventBus.getDefault().post(new CartEvent());
                MyUtil.mytoast0(mContext, "添加成功", R.mipmap.ic_sure);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }


}
